import config from '../config/config.js';
const request = (url = '', data = {}, page = '/pages/index/index', type = 'POST', header = {}) => {
	let {serverAddress} = config;
	
	return new Promise((resolve, reject) => {
		// token 放到header头中的 
		let token = uni.getStorageSync('token');
		if(token){
			let authorization = "Bearer " + token;
			header.Authorization = authorization;
		}
		uni.request({
			method: type,
			url: serverAddress + url,
			data: {
				...data
			},
			header: header,
			dataType: 'json',
		}).then((response) => {
			let {data, code,msg} = response;
			if(data.code == 401){
				console.log("request 401 : ", url)
				// resolve(data);
				/*#ifdef MP*/
				if(page != '/pages/my/my'){
					setTimeout(()=>{
						uni.clearStorageSync()
						uni.reLaunch({
							url: page
						})
					}, 500)
				}
				resolve(data);
				/*#endif*/
			}else{
				resolve(data);
			}
		}).catch(error => {
			let {err, res} = error;
			reject(err)
		})
	});
}
export default request